package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lsx
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程id查询章节小节VO对象
     * @param courseId
     * @return 章节小节的VO对象
     */
    @GetMapping("getChapterVideo/{courseId}")//只有课程中才有章节和小结 需要根据课程id进行查询
    public  R getChapterVideo(@PathVariable String courseId){

        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    /**
     * 添加章节
     * @param eduChapter
     * @return
     */
    @PostMapping("/addChapter")
    public  R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    /**
     * 根据id查询章节
     * @param chapterId
     * @return
     */
    @GetMapping("/getChapterById/{chapterId}")
    public  R getChapterById(@PathVariable String chapterId){
        EduChapter chapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }

    /**
     * 更新章节信息
     * @param eduChapter
     * @return
     */
    @PostMapping("/updateChapter")
    public  R updateChapter(@RequestBody EduChapter eduChapter ){
         eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 根据id删除章节信息
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public  R deleteChapter(@PathVariable String id){
        boolean flag = eduChapterService.deleteById(id);
        return R.ok().data("flag",flag);
    }

}

